﻿package com.shootingGame.renderableClasses
{
	import com.common.physics.Vector2;
	import com.common.physics.Velocity;
	
	import flash.display.MovieClip;
	import flash.utils.getQualifiedClassName;

	public class MobileObject extends GameObject
	{
		private var _maxSpeed : Number;
		//range(-1, 1)
		private var _thrustX : Number;
		//range(-1, 1)
		private var _thrustY : Number;
		private var _velocity : Velocity;
		private var _acceleration : Number;
		private var _deceleration : Number;
				
		public function MobileObject(model:MovieClip, xPos:Number=0, yPos:Number=0, max_Speed:Number = 0, acceleration:Number = 0, deceleration:Number = 0)
		{
			super(model, xPos, yPos);
			thrustX = 0;
			thrustY = 0;
			_velocity = new Velocity();
			_maxSpeed = max_Speed;
			_acceleration = acceleration;
			_deceleration = deceleration;
			
		}
		
		override public function update():void{
			if(isExpired)
				return;
			super.update();
			var velocityVector:Vector2;
			updateVelocity();
			velocityVector = _velocity.getVector();
			memoryX += velocityVector.x;
			memoryY += velocityVector.y;
			updateThrust();
			
		}

		virtual protected function updateThrust():void{
			
		}

		virtual protected function updateVelocity():void{
			if(!(maxSpeed == 0)){
				velocityDirection = Math.atan2(_thrustY, _thrustX)/Math.PI * 180;
				if(Math.abs(thrustX) >= Math.abs(thrustY))
					speed = Math.abs(thrustX * maxSpeed);
				else
					speed = Math.abs(thrustY * maxSpeed);
			}
		}
		
		public function resetVelocity():void{
			speed = 0;
			velocityDirection = 0;
		}
		
		public function get thrustX() : Number{
			return _thrustX;
		}
		
		public function set thrustX(value : Number) {
			if(value > 1)
				_thrustX = 1;
			else
				if(value < -1)
					_thrustX = -1;
				else
					_thrustX = value;
		}
		
		public function get thrustY() : Number{
			return _thrustY;			
		}
		
		public function set thrustY(value : Number) {
			if(value > 1)
				_thrustY = 1;
			else
				if(value < -1)
					_thrustY = -1;
				else
					_thrustY = value;
		}
		
		public function get speed() : Number{
			return _velocity.speed;
		}
		
		public function set speed(value:Number){
			if(maxSpeed == 0)
				_velocity.speed = value;
			else{
				if(value > maxSpeed)
					_velocity.speed = maxSpeed;
				else
					if(value < 0)
						_velocity.speed = 0;
					else
						_velocity.speed = value;
			}
		}
		
		public function get velocityDirection() : Number{
			return _velocity.direction;
		}
		
		public function set velocityDirection(value : Number) {
			if(value >= 360){
				value = value % 360;
			}
			else
				if(value < 0){
					while(value < 0)
						value += 360;
				}
			_velocity.direction = value;				
		}
		
		public function get maxSpeed() : Number{
			return _maxSpeed;
		}
		
	}
}
